<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Monhoc_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
	
	// get all list mon hoc from db
	function get_monhoc_info_by_id($id)
	{
		$this->db->select('*');
		$this->db->from('monhoc');
		$this->db->where('id', $id);
		$query = $this->db->get();
		return $query->row();
	}
	
	// get all list giao vien from db
	function get_all_giaovien()
	{
		$this->db->select('id, magv, tengv');
		$this->db->from('giaovien');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// get all list khoa from db
	function get_all_khoa()
	{
		$this->db->select('id, makhoa, tenkhoa');
		$this->db->from('khoa');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// get all list mon hoc from db
	function get_all_monhoc($limit, $start)
	{
		$this->db->select('id, mamon, tenmon, sotinchi, lythuyet,thuchanh, status');
		$this->db->from('monhoc');
		$this->db->order_by('tenmon','asc');
		$this->db->limit($limit, $start);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function add_monhoc()
	{
		$data = array(
			'mamon' => $this->input->post('mamon'),
			'tenmon' => $this->input->post('tenmon'),
			'lythuyet' => $this->input->post('lythuyet'),
			'thuchanh' => $this->input->post('thuchanh'),
			'sotinchi' => $this->input->post('sotinchi'),
			'id_khoa' => $this->input->post('khoa'),
			'created_by' =>$this->user->id,
			'status' => $this->input->post('status'),
		);
		$this->db->set($data);
		$this->db->insert('monhoc');
		//$idmon = $this->insert_id();
	}
	
	function edit_monhoc($id)
	{
		$data = array(
				'tenmon' => $this->input->post('tenmon'),
				'lythuyet' => $this->input->post('lythuyet'),
				'thuchanh' => $this->input->post('thuchanh'),
				'sotinchi' => $this->input->post('sotinchi'),
				'id_khoa' => $this->input->post('khoa'),
				'edited_by' =>$this->user->id,
				'status' => $this->input->post('status'),
		);
		$this->db->set($data);
		$this->db->where('id',$id);
		$this->db->update('monhoc');
	}
	
	public function search_subject($keyword)
	{
		$this->db->select('*');
		$this->db->from('monhoc');
		$this->db->like('mamon', $keyword);
		$this->db->or_like('tenmon', $keyword);
		$this->db->order_by('id', 'DESC');
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
		else
		{
			return null;
		}
	}
	
	// dem tong so mon hoc hien co
	function count_all_monhoc($status = '')
	{
		$this->db->select('id');
		$this->db->from('monhoc');
		$this->db->where('deleted',0);
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		return $this->db->count_all_results();
	}
	public function monhoc_exchange($id, $status = 0)
	{
		$data = array(
				'status' => $status,
		);
	
		$this->db->where('id', $id);
		$this->db->update('monhoc', $data);
		if($this->db->affected_rows() >= 0)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
}

/* Location: ./application/models/monhoc_model.php */